Skip to content

Conversation

@CrosRoad95
Copy link

@CrosRoad95 CrosRoad95 commented Dec 30, 2020

Option to execute custom time consuming tasks without blocking main thread.

current functions list, its syntax and purpose:

  • createThread(string code) -- Creates a new thread and start executing code which in that thread
  • getThreadState(thread) -- Return current state of thread
  • threadCall(thread, functionName, table of arguments) -- calling function inside thread.
  • killThread(thread, terminate) -- sends signal to kill thread, wait 1 second ( if second parametr is false ) and then terminates a thread forcefully, ending its execution.
  • getThreadFailureReason(thread) -- In case of error thrown inside
  • threadGetResult(thread) -- Returns value thread returned at the end ( if something have been returned and code reached end )
  • getThreads() -- return all threads
  • setThreadShutdownHandler(thread, callback) -- when "exit" get called inside thread

Function availiable only in thread:

  • sleep(ms) --
  • exit(...) -- stopping thread, send data to shutdown handler
  • sendMessage(...) -- sends message that you can catch using setThreadCustomHandler in main thread

Maybe:

  1. bool isMainThread() -- return true if called in main thread, otherwise false.
  2. setThreadOutputHandler(thread, callback) -- to set where are outputs from print, outputChatBox ect. will redirected,
  3. setThreadErrorHandler(thread, callback) -- to set where to output errors
  4. setThreadCustomHandler(thread, callback) -- used for communication from thread, to main thread ( i don't have idea how to name it )
  5. threadLoadstring(thread, code) -- to load extra code into thread ( if while creating you explicit allow it in options )
  6. getThreadsLimits() -- Clientside, returns information about user settings, how many threads can be created ect.
  7. getThreadtimings(thread) -- https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimes
  8. startThread(thread) -- Starts the thread.
  9. pauseThread(thread) -- Pause the thread. -just calling "sleep" function all time until "start" get called

Maybe in future, option to create thread-safe variable, name based on std::atomic

  1. atomic createAtomic( ... )
  2. values getAtomicValue( atomic )
  3. bool setAtomicValue( ... )

Other:

  1. "threadCall" wil have option to get return of that function, but due problems with argument parser right now you can't do that.
  2. while creating new thread, you should have options to a bit more customize it. I'm consider that useful functions would be to set what will loaded by default, what functions, if possible, option to change "speed" of that thread by adding sleep(1) every n instructions,
  3. Clientside threads are a bit dangerous eg. malicious code that will run infinity loop to consume all cpu time. Client should have option to somehow limit, slowdown custom threads
  4. Only thread-safe functions should be availiable inside
  5. Errors, how to handle them? i wrote proposal function above
  6. Should we limit threads serverside, add mtaserver.conf option to limit how many of them can running at the same time? or you can create as many threads as you want?
  7. ACL, by default all functions should be disallowed. When thread get created, it should inherit permissions from resource that created this thread

Current example:

code = [[
function mineBitcoins()
    for i=1,500000000 do

    end
    print("1 bitcoined nined")
end
function add(a,b)
    print("add",a,b)
    return a+b
end
print("hello, i'm thread")
return 1,2,3
]]

addEventHandler("onResourceStart", resourceRoot, function()
    thread = createThread(code)
    local state = ""
    i = 0
    function update()
        i = i + 1
        local newState = getThreadState(thread)
        if(state ~= newState)then
            state = newState
            print("state =", state)
        end
        if(state == "loaded")then
            print("result",getThreadResult(thread))
            threadCall(thread, "add", {i, 10})
            threadCall(thread, "mineBitcoins")
        end
    end
    update()
    setTimer(update,0,0)
end)

image

@patrikjuvonen patrikjuvonen linked an issue Dec 30, 2020 that may be closed by this pull request
@patrikjuvonen patrikjuvonen added the enhancement New feature or request label Dec 30, 2020
@Einheit-101
Copy link

This is cool.

@prnxdev
Copy link

prnxdev commented Jan 5, 2021

OOP support pls

@Deihim007
Copy link
Contributor

Is it possible to change createThread(string code) to createThread(function) ?

@CrosRoad95
Copy link
Author

Is it possible to change createThread(string code) to createThread(function) ?

yes

@gta191977649
Copy link
Contributor

Is it available on serverside or clientside?

@Lpsd
Copy link
Member

Lpsd commented Mar 29, 2021

Both, refer to the code

@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2022

This draft pull request is stale because it has been open for at least 90 days with no activity. Please continue on your draft pull request or it will be closed in 30 days automatically.

@github-actions github-actions bot added the stale Inactive for over 90 days, to be closed label Jan 7, 2022
@github-actions github-actions bot closed this Feb 7, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2022

This draft pull request was closed because it has been marked stale for 30 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request stale Inactive for over 90 days, to be closed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add multi-threading support to Lua

7 participants